package com.dingdang.controller.car_detail_management;


import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.BasicParameter;
import com.dingdang.mapper.BasicParameterMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//基本参数表
@RestController
@RequestMapping("basicParameter")
@Api("车辆基本参数管理接口")
public class BasicParameterController
{
    @Autowired
    private BasicParameterMapper basicParameterMapper;

    @GetMapping
    @ApiOperation("车辆基本参数搜索")
    @ApiImplicitParam(name = "void",value = "空",required = true,paramType = "query")
    public Result getCarGoods()
    {
        try
        {
            List<BasicParameter> basicParameterMapperList = basicParameterMapper.selectList(null);
            return Result.response(ResultEnum.SUCCESS).setData(basicParameterMapperList);
        }
        catch (Exception e)
        {
            return Result.response(ResultEnum.ERROR).setData(e);
        }

    }

    //车辆基本参数增加
    @PostMapping
    @ApiOperation("车辆基本参数增加")
    @ApiImplicitParam(name = "BasicParameter",value = "车辆基本参数实体类",required = true,paramType = "body")
    public Result insertAdmin(@RequestBody BasicParameter basicParameter)
    {
        int i = basicParameterMapper.insert(basicParameter);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    //车辆基本参数回显
    @PostMapping("/adminToUpdate")
    @ApiOperation("车辆基本参数回显")
    @ApiImplicitParam(name = "id",value = "车辆基本参数id",required = true,paramType = "query")
    public Result ToUpdateAdmin(@RequestParam("id") Long id)
    {
        BasicParameter basicParameter = new BasicParameter();
        basicParameter = basicParameterMapper.selectById(id);
        return Result.response(ResultEnum.SUCCESS).setData(basicParameter);
    }

    //管理员修改
    @PutMapping
    @ApiOperation("车辆基本参数")
    @ApiImplicitParam(name = "TbCarGoods",value = "车辆基本信息实体类",required = true,paramType = "body")
    public Result UpdateAdmin(@RequestBody BasicParameter basicParameter)
    {
        int i = basicParameterMapper.updateById(basicParameter);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @DeleteMapping()
    @ApiOperation("车辆基本参数删除")
    @ApiImplicitParam(name = "id",value = "车辆id",required = true,paramType = "query")
    public Result deleteInfo(@RequestParam(value = "id") Long []ids)
    {
        List<Long> deleteBatch = new ArrayList<>();
        for (Long addr:ids)
        {
            deleteBatch.add(addr);
        }
        int i = basicParameterMapper.deleteBatchIds(deleteBatch);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }
}
